package poi.excel;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: TODO
 * @author admin
 * @date 2024/4/19 14:19
 * @version 1.0
 */
public class YBWTTest {


    public static void main(String[] args) {
        Workbook workbook = ExcelUtils.readExcel("D:\\document\\医保平台\\四川省人民医院（四川省红十字医院）2023市医保.xlsx");
        Sheet sheet = workbook.getSheetAt(0);
        int rowCount = sheet.getLastRowNum();
        System.out.println("总行数：" + rowCount);
        Map<String, List<String>> wtArray = new HashMap<>();

        Map<String, List<String>> yclArray = new HashMap<>();

        for (int i = 0; i < rowCount; i++) {
            if (i == 0) {
                continue;
            }
            Row row = sheet.getRow(i);
            String zkjg = String.valueOf(ExcelUtils.getCellFormatValue(row.getCell(15)));
            Map<String, String> wtMap = new HashMap<>();
            wtMap.put("116", "中医主病诊断名称与代码填写不规范");
            wtMap.put("137", "新生儿入院体重填写不合理");
            wtMap.put("138", "新生儿出生体重填写不合理");
            wtMap.put("2238", "新生儿(天龄小于28天)入院时，新生儿入院体重必须填写");
            wtMap.put("2279", "新生儿(天龄小于28天)入院时，新生儿出生体重必须填写");
            wtMap.put("2240", "诊断编码中含有Z37.0/Z37.2/Z37.3/Z37.5/Z37.6，新生儿出生体重或多新生儿出生体重为空");
            wtMap.put("2241", "新生儿出生体重或多新生儿出生体重为空");

            for (String key : wtMap.keySet()) {
                if (zkjg.contains(wtMap.get(key))) {
                    String jsId = String.valueOf(ExcelUtils.getCellFormatValue(row.getCell(5)));
//                    System.out.println(wtMap.get(key) + "符合条件ID" + jsId);
                    if (!wtArray.containsKey(wtMap.get(key))) {
                        List<String> list = new ArrayList<>();
                        wtArray.put(wtMap.get(key), list);
                    }
                    //拿到id写入对应的集合
                    List<String> list = wtArray.get(wtMap.get(key));
                    list.add(jsId);
                    wtArray.put(wtMap.get(key), list);

                }
            }
        }
        //将符合条件的结算id写在本地
        for (String fileName : wtArray.keySet()) {
            File file = new File("D:\\document\\医保平台\\" + fileName + ".txt");
            BufferedWriter bufferedWriter = null;
            try {
                if (!file.exists()) {
                    file.createNewFile();
                } else {
                    file.delete();
                    file.createNewFile();
                }
                bufferedWriter = new BufferedWriter(new FileWriter(file));
                List<String> wtjsid = wtArray.get(fileName);
                for (String s : wtjsid) {
                    bufferedWriter.write(s);
                    bufferedWriter.newLine();
                    bufferedWriter.flush();
                }
                bufferedWriter.close();
            } catch (IOException e) {
                System.out.println("创建文件失败");
            } finally {
            }


            List<String> wtjsid = wtArray.get(fileName);
            for (String jsid : wtjsid) {
                //todo 这里处理重传逻辑
                try {

                } catch (Exception e) {
                    //重传失败写入本地
                    System.out.println("结算ID【{}】处理失败" + jsid);
                    if (!yclArray.containsKey(fileName)) {
                        List<String> list = new ArrayList<>();
                        yclArray.put(fileName, list);
                    }
                    List<String> list = yclArray.get(fileName);
                    list.add(jsid);
                    yclArray.put(fileName, list);
                }
            }

        }
        //todo 将已处理的集合写入本地文件
        if(yclArray.isEmpty()){
            for (String fileName : yclArray.keySet()) {
                yclArray.get(fileName);

            }
        }
    }
}
